﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq.Expressions;
using ExpressionEvaluator;
using ExpressionEvaluator.Expceptions;

namespace ExpressionEvalTest
{
	[TestClass]
	public class SyntaxErrors
	{
		[TestMethod]
		public void ExecutableErrors()
		{
			CallExpressionEval<Func<int>>(ParserState.Executable, "1 1", null);
			CallExpressionEval<Func<int>>(ParserState.BracketEnd, "(a)a", null);
			CallExpressionEval<Func<int>>(ParserState.Text, "\"a\"a", null);
			CallExpressionEval<Func<int>>(ParserState.IndexerEnd, "a[1]a", null);
		}

		private static void CallExpressionEval<TDelegate>(ParserState lastParserState, string code, params ParameterExpression[] parameters)
		{
			Exception catchedException = null;
			try
			{
				var Evaluator = new ExpressionEval(code);
				if (parameters != null)
				{
					foreach (ParameterExpression param in parameters)
					{
						Evaluator.AddParam(param);
					}
				}
				Expression eval = Evaluator.BuildTree();
			}
			catch (Exception e)
			{
				catchedException = e;
			}
			Assert.IsNotNull(catchedException, "No exception generated by Eval!");
			Assert.IsInstanceOfType(catchedException, typeof(SyntaxException), catchedException.ToString());
			Assert.AreEqual((catchedException as SyntaxException).PreviousState, lastParserState, catchedException.ToString());
		}
	}
}
